#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import glob
import optparse
import os
import os.path
import re
import subprocess
import sys
import j264_utils

#~ Walk input directory for VDR recordings.
def processVideoFiles( fileList ):
    for videoDir in fileList.keys():
        for videoFile in fileList[videoDir]:
            # Generate the prefix.
            parts = videoDir.split( os.path.sep )
            programName = parts[-2]
            airDate = parts[-1]
            prefix = programName + '_' + airDate[:10]
            #~ Process video files.
            src = videoDir + os.path.sep + videoFile
            tgt = videoDir + os.path.sep + prefix + videoFile
            # Remove 'cut_', and 'muxed' parts from the string.
            tgt = tgt.replace( "cut_", "" ).replace( "muxed", "" )
            print( "mv -i", src, tgt )
            # Check if there is a info.vdr to process as well.
            sourceInfoFile = videoDir + os.sep + 'info.vdr'
            targetInfoFile = videoDir + os.sep + prefix + '_info.vdr'
            if os.path.exists( sourceInfoFile ):
                print( "mv -i", sourceInfoFile, targetInfoFile )

#~ 'Main'
if __name__ == "__main__":
    if len( sys.argv ) < 2:
        print( "Gimme some directories to work on!" )
        sys.exit( 2 )
    # Process input directories.
    for inputDir in sys.argv[1:]:
        j264_utils.processInputDir( os.path.abspath( inputDir ), "cut_.*\.mp4", processVideoFiles )
